package com.tsx.demo;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.tsx.utils.PdfFontUtil;

import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


public class DrawEllipseWithText {
    public static void main(String[] args) throws Exception {
        // 1. 创建PDF文档
        String dest = Paths.get(Constants.dest, "test.pdf").toString();
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdf = new PdfDocument(writer);
        pdf.addNewPage();
        Document document = new Document(pdf);

        // 2. 创建一个矩形区域用于绘制椭圆
        Rectangle rectangle = new Rectangle(100, 400, 200, 100); // 更大的椭圆

        // 3. 创建Canvas并绘制椭圆
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.getFirstPage());

        // 保存当前画布状态
        pdfCanvas.saveState();

        // 计算旋转中心点（椭圆中心）
        float centerX = rectangle.getLeft() + rectangle.getWidth() / 2;
        float centerY = rectangle.getBottom() + rectangle.getHeight() / 2;

        // 创建30度旋转的变换矩阵
        double angle = Math.toRadians(20); // 30度转换为弧度
        double cos = Math.cos(angle);
        double sin = Math.sin(angle);

        // 应用旋转变换（先平移至原点，旋转，再平移回原位置）
        pdfCanvas.concatMatrix(cos, sin, -sin, cos,
                centerX - centerX * cos + centerY * sin,
                centerY - centerX * sin - centerY * cos);

        // 绘制椭圆
        pdfCanvas.setStrokeColor(ColorConstants.RED)
                .setLineWidth(2)
                .ellipse(rectangle.getLeft(), rectangle.getBottom(),
                        rectangle.getRight(), rectangle.getTop())
                .stroke();

        // 4. 绘制两条水平分隔线
        float y1 = rectangle.getBottom() + rectangle.getHeight() * 2 / 3;
        float y2 = rectangle.getBottom() + rectangle.getHeight() / 3;

        pdfCanvas.setStrokeColor(ColorConstants.RED)
                .setLineWidth(2)
                .moveTo(rectangle.getLeft() + 6, y1)
                .lineTo(rectangle.getRight() - 6, y1)
                .stroke()
                .moveTo(rectangle.getLeft() + 6, y2)
                .lineTo(rectangle.getRight() - 6, y2)
                .stroke();

        // 5. 在三个部分添加文字
        Canvas canvas = new Canvas(pdfCanvas, rectangle);

        PdfFont pdfFont = PdfFontUtil.chinesePdfFont();
        // 上部文字
        Paragraph topText = new Paragraph("张三")
                .setFontColor(ColorConstants.RED)
                .setFont(pdfFont)
                .setFontSize(16);
        canvas.showTextAligned(topText,
                rectangle.getLeft() + rectangle.getWidth() / 2,
                rectangle.getTop() - rectangle.getHeight() / 6,
                TextAlignment.CENTER, VerticalAlignment.MIDDLE);

        // 中部文字
        Paragraph middleText = new Paragraph("编辑上传")
                .setFontColor(ColorConstants.RED)
                .setFont(pdfFont)
                .setFontSize(22);
        canvas.showTextAligned(middleText,
                rectangle.getLeft() + rectangle.getWidth() / 2,
                rectangle.getBottom() + rectangle.getHeight() / 2,
                TextAlignment.CENTER, VerticalAlignment.MIDDLE);

        // 下部文字
        LocalDate now = LocalDate.now();
        String date = now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        Paragraph bottomText = new Paragraph(date)
                .setFontColor(ColorConstants.RED)
                .setFont(pdfFont)
                .setFontSize(16);
        canvas.showTextAligned(bottomText,
                rectangle.getLeft() + rectangle.getWidth() / 2,
                rectangle.getBottom() + rectangle.getHeight() / 6,
                TextAlignment.CENTER, VerticalAlignment.MIDDLE);

        // 恢复画布状态（取消旋转）
        pdfCanvas.restoreState();


        // 6. 关闭文档
        document.close();

        System.out.println("PDF创建成功: " + dest);
    }
}